﻿/*
 *  Sqlite ORM - PUBLIC DOMAIN LICENSE
 *  Copyright (C)  2010-2012. Ian Quigley
 *  
 *  This source code is provided 'As is'. You bear the risk of using it.
 */

using System;
using SqliteORM.Tests.Model;
using Xunit;
using SubSpec;

namespace SqliteORM.Tests
{
	public class CreateTable
	{
		[Specification]
		public void CreateOneSimpleTable()
		{
            "Given an empty database"
                .Context(() =>
                {
                    Context.Initialise();
                    DbConnection.Initialise(Context.ConnectionString);
                });

            "when we open a table ".Do(
                () =>
                {
                    using (TableAdapter<TableIdString> tableIdString = TableAdapter<TableIdString>.Open())
                    { }
                });

            "expect it exists".Assert(
                () => Assert.Contains("TableIdString", Context.TableNames()) );
		}

		[Specification]
		public void CreateTableWithStringPkAndAddData()
		{
			"Given an empty database"
				.Context(() =>
				{
					Context.Initialise();
					DbConnection.Initialise(Context.ConnectionString);
				});

			"when we open a table with Name and PK attributes ".Do(
				() =>
				{
					using (TableAdapter<TableAttributeTest> adapter = TableAdapter<TableAttributeTest>.Open())
					{
						adapter.CreateUpdate( "one" );
						adapter.CreateUpdate("two");
					}
				});

			"expect table exists".Assert(
				() => Assert.Contains("SomeOtherName", Context.TableNames()) );

			"expect data exists".Assert(
				() =>
				{
					using (TableAdapter<TableAttributeTest> adapter = TableAdapter<TableAttributeTest>.Open())
					{
						Assert.NotNull( adapter.Read( "one" ) );
						Assert.NotNull( adapter.Read( "two" ) );
					}
				});
		}

        [Specification]
        public void CreateTableWithParamValue()
        {
            "Given an empty database"
                .Context(() =>
                {
                    Context.Initialise();
                    DbConnection.Initialise(Context.ConnectionString);
                });

            "when we open a table with param ".Do(
                () =>
                {
                    using (TableAdapter<TableParam> tableParam = TableAdapter<TableParam>.Open(23))
                    { }
                });

            "expect that the table exists".Assert(
                () => Assert.Contains("TableParam_23", Context.TableNames()) );
        }

		[Specification]
		public void CreateTableWithFields()
		{
			"Given an empty database"
				.Context(() =>
				{
					Context.Initialise();
					DbConnection.Initialise(Context.ConnectionString);
				});

			Exception ex = default(Exception);
			"when we open a table with only fields ".Do(
				() => ex = Record.Exception( ( ) =>
				                        	{
												using (var tableParam = TableAdapter<TableWithFields>.Open())
				                        		{}
				                        	} ));

			"expect that it does not throw an exception".Assert(
				() => Assert.Null( ex ) );
		}



		[Specification]
		public void CreateTableWithDuplicateFieldParam()
		{
			"Given an empty database"
				.Context(() =>
				{
					Context.Initialise();
					DbConnection.Initialise(Context.ConnectionString);
				});

			Exception ex = default(Exception);
			"when we open a table duplicate field ".Do(
				() => ex = Record.Exception(() =>
				{
					using (TableAdapter<TableInvalid2> tableParam = TableAdapter<TableInvalid2>.Open())
					{
					}
				}));

			"expect that it throws an exception".Assert(
				() => Assert.NotNull(ex));
		}

		[Specification]
		public void CreateTableWithUnsupportedType()
		{
			"Given an empty database"
				.Context(() =>
				{
					Context.Initialise();
					DbConnection.Initialise(Context.ConnectionString);
				});

			Exception ex = default(Exception);
			"when we open a table with unsupported type".Do(
				() => ex = Record.Exception(() =>
				{
					using (TableAdapter<TableInvalid3> tableParam = TableAdapter<TableInvalid3>.Open())
					{
					}
				}));

			"expect that it throws an exception".Assert(
				() => Assert.NotNull(ex));
		}
	
		[Specification]
		public void CreateTableWithTooManyArgs()
		{
			"Given an empty database"
				.Context(() =>
				{
					Context.Initialise();
					DbConnection.Initialise(Context.ConnectionString);
				});

			Exception ex = default(Exception);
			"when we open a table with two many args ".Do(
				() => ex = Record.Exception( ( ) =>
				                        	{
												using (TableAdapter<TableIdString> tableParam = TableAdapter<TableIdString>.Open(3))
				                        		{
				                        		}
				                        	} ));

			"expect that it throws an exception".Assert(
				() => Assert.NotNull( ex ) );
		}
	}
}
